unit Unit60;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  DBGrids, LR_Class, LR_DBSet, Unit1, Unit22, Unit36, sqldb, db;

type

  { TForm60 }

  TForm60 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    CheckBox1: TCheckBox;
    ComboBox1: TComboBox;
    Datasource1: TDatasource;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    frDBDataSet1: TfrDBDataSet;
    frReport1: TfrReport;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    SQLQuery1: TSQLQuery;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure frReport1GetValue(const ParName: String; var ParValue: Variant);
  private
    tipo_venta : string;
    total : double;
    con_deuda : string;
    saldo_deudor : string;
  public
    { public declarations }
  end; 

var
  Form60: TForm60;

implementation

{$R *.lfm}

{ TForm60 }

procedure TForm60.Button1Click(Sender: TObject);
begin
  saldo_deudor := '';

  if CheckBox1.Checked = true then
  begin
    con_deuda := ' and vta_saldo > 0 ';
    saldo_deudor := '(solo ventas con saldo deudor)';
  end;

  if SQLQuery1.Active = True then
    SQLQuery1.Close;

  SQLQuery1.SQL.Text := 'select * from vw_listado_productos_comprados where cln_nombre like ''' + Edit4.Text
    + ''' and vta_tipo like ''' + tipo_venta + ''' and vta_fecha >= ''' + Edit1.Text + ''' and vta_fecha <= '''
    + Edit2.Text + '''' + con_deuda + ' order by vta_fecha';

  SQLQuery1.Open;

  total := 0;

  while not SQLQuery1.EOF do
  begin
    total := total + SQLQuery1.Fields[7].AsFloat;
    SQLQuery1.Next;
  end;

  frReport1.LoadFromFile('articulos.lrf');
  frReport1.ShowReport;

  Close;
end;

procedure TForm60.Button2Click(Sender: TObject);
begin
  Form22.ShowModal;

  Edit1.Text := leftStr(Form22.fecha_desde, 10);
  Edit2.Text := leftStr(Form22.fecha_hasta, 10);

  if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '') and (ComboBox1.Text <> '') then
    Button1.Enabled := True;

  Button3.SetFocus;
end;

procedure TForm60.Button3Click(Sender: TObject);
begin
  Form36.ShowModal;

  if Form36.codigo <> '' then
  begin
    Edit3.Text := Form36.codigo;
    Edit4.Text := Form36.nombre;
  end;

  if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '') and (ComboBox1.Text <> '') then
    Button1.Enabled := True;

  ComboBox1.SetFocus;
end;

procedure TForm60.Button4Click(Sender: TObject);
begin
  Close;
end;

procedure TForm60.ComboBox1Change(Sender: TObject);
begin
  if ComboBox1.Text = 'TODOS' then
    tipo_venta := '%'
  else
    tipo_venta := ComboBox1.Text;

  if ComboBox1.Text = 'CTACTE' then
  begin
    CheckBox1.Visible := true;
    CheckBox1.Checked := false;
  end
  else
  begin
    CheckBox1.Visible := false;
    CheckBox1.Checked := false;
  end;

  if (Edit1.Text <> '') and (Edit2.Text <> '') and (Edit3.Text <> '') and (Edit4.Text <> '') and (ComboBox1.Text <> '') then
    Button1.Enabled := True;
end;

procedure TForm60.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
  if SQLQuery1.Active = true then
    SQLQuery1.Close;
end;

procedure TForm60.FormCreate(Sender: TObject);
begin
  SQLQuery1.DataBase := Form1.PQConnection1;
  SQLQuery1.Transaction := Form1.SQLTransaction1;
end;

procedure TForm60.FormShow(Sender: TObject);
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
  Edit4.Text := '';
  ComboBox1.Text := '';
  con_deuda := '';

  CheckBox1.Checked := false;
  CheckBox1.Visible := false;

  Button1.Enabled := False;

  Button2.SetFocus;
end;

procedure TForm60.frReport1GetValue(const ParName: String; var ParValue: Variant
  );
begin
  if ParName = 'saldo_deudor' then
    ParValue := saldo_deudor;

  if ParName = 'nombre_cliente' then
    ParValue := Edit4.Text;

  if ParName = 'fecha_desde' then
    ParValue := Edit1.Text;

  if ParName = 'fecha_hasta' then
    ParValue := Edit2.Text;

  if ParName = 'tipo_venta' then
    ParValue := ComboBox1.Text;

  if ParName = 'total' then
    ParValue := FloatToStr(total);
end;

end.

